# Set console encoding to UTF-8
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8

Write-Host "========================================" -ForegroundColor Green
Write-Host "Preparing to upload project to Gitee..." -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green

# Check if Git is installed
try {
    $gitVersion = git --version 2>$null
    if ($LASTEXITCODE -eq 0) {
        Write-Host "Git is installed: $gitVersion" -ForegroundColor Green
    } else {
        throw "Git not installed"
    }
} catch {
    Write-Host "Git is not installed!" -ForegroundColor Red
    Write-Host "Please visit https://git-scm.com/download/win to download and install Git" -ForegroundColor Yellow
    Write-Host "After installation, run this script again" -ForegroundColor Yellow
    Read-Host "Press any key to exit"
    exit 1
}

# Check current directory
$currentDir = Get-Location
Write-Host "Current directory: $currentDir" -ForegroundColor Cyan

# Check if Git repository already exists
if (Test-Path ".git") {
    Write-Host "Git repository already exists, skipping initialization" -ForegroundColor Yellow
} else {
    # Initialize Git repository
    Write-Host "Initializing Git repository..." -ForegroundColor Cyan
    git init
    if ($LASTEXITCODE -ne 0) {
        Write-Host "Git initialization failed!" -ForegroundColor Red
        Read-Host "Press any key to exit"
        exit 1
    }
    Write-Host "Git repository initialized successfully!" -ForegroundColor Green
}

# Add all files
Write-Host "Adding files to staging area..." -ForegroundColor Cyan
git add .
if ($LASTEXITCODE -ne 0) {
    Write-Host "Failed to add files!" -ForegroundColor Red
    Read-Host "Press any key to exit"
    exit 1
}
Write-Host "Files added successfully!" -ForegroundColor Green

# Check if there are files to commit
$status = git status --porcelain
if ([string]::IsNullOrEmpty($status)) {
    Write-Host "No files to commit" -ForegroundColor Yellow
} else {
    # Commit code
    Write-Host "Committing code..." -ForegroundColor Cyan
    git commit -m "Initial commit: Django project"
    if ($LASTEXITCODE -ne 0) {
        Write-Host "Failed to commit code!" -ForegroundColor Red
        Read-Host "Press any key to exit"
        exit 1
    }
    Write-Host "Code committed successfully!" -ForegroundColor Green
}

Write-Host "========================================" -ForegroundColor Green
Write-Host "Local Git repository is ready!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""

Write-Host "Next steps to complete manually:" -ForegroundColor Yellow
Write-Host ""

Write-Host "Method 1: Using Command Line" -ForegroundColor Cyan
Write-Host "1. Login to your Gitee account" -ForegroundColor White
Write-Host "2. Click '+' in top right corner → 'New Repository'" -ForegroundColor White
Write-Host "3. Fill in repository information:" -ForegroundColor White
Write-Host "   - Repository name: mybabys-project" -ForegroundColor White
Write-Host "   - Description: My Django project" -ForegroundColor White
Write-Host "   - Select 'Public'" -ForegroundColor White
Write-Host "   - Do NOT check 'Initialize with README'" -ForegroundColor White
Write-Host "4. After creating repository, copy the repository URL" -ForegroundColor White
Write-Host "5. Run these commands in terminal:" -ForegroundColor White
Write-Host "   git remote add origin [your-repository-url]" -ForegroundColor Green
Write-Host "   git push -u origin master" -ForegroundColor Green
Write-Host ""

Write-Host "Method 2: Using Gitee Desktop (Recommended)" -ForegroundColor Cyan
Write-Host "1. Download and install Gitee Desktop:" -ForegroundColor White
Write-Host "   https://gitee.com/oschina/git-osc/wikis/Help/Doc/Git%E5%AE%89%E8%A3%85%E5%8C%85%E4%B8%8B%E8%BD%BD" -ForegroundColor Blue
Write-Host "2. Open Gitee Desktop" -ForegroundColor White
Write-Host "3. Login to your Gitee account" -ForegroundColor White
Write-Host "4. Click 'Clone/Download' → 'New Repository'" -ForegroundColor White
Write-Host "5. Select current folder: $currentDir" -ForegroundColor White
Write-Host "6. Fill in repository information and submit" -ForegroundColor White
Write-Host ""

Write-Host "Project files are ready:" -ForegroundColor Green
Write-Host "✅ .gitignore - Ignore unnecessary files" -ForegroundColor Green
Write-Host "✅ README.md - Project documentation" -ForegroundColor Green
Write-Host "✅ requirements.txt - Project dependencies" -ForegroundColor Green
Write-Host "✅ upload_to_gitee.bat - Batch script" -ForegroundColor Green
Write-Host "✅ upload_to_gitee.ps1 - PowerShell script" -ForegroundColor Green
Write-Host ""

Read-Host "Press any key to exit" 